package classes.gui 
{
	import classes.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.net.*;
	
	import by.blooddy.crypto.serialization.*;
	
	/**
	 * ...
	 * @author yanmasharski
	 */
	
	public class TwoPhotosInterface extends TwoPhotosInterface_NS {
		
		private var file_reference_one:FileReference;
		private var file_reference_two:FileReference;
		private const file_reference_type:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
		
		public var photo_container_one:Sprite;
		public var photo_container_two:Sprite;
		
		
		
		public function TwoPhotosInterface() {
			file_reference_one = new FileReference();
			file_reference_two = new FileReference();
			file_reference_one.addEventListener(Event.COMPLETE, photoLoaded);
			file_reference_two.addEventListener(Event.COMPLETE, photoLoaded);
			file_reference_one.addEventListener(Event.SELECT, photoSelected);
			file_reference_two.addEventListener(Event.SELECT, photoSelected);
			
			load_btn_one.addEventListener(MouseEvent.CLICK, loadBtnClick);
			load_btn_two.addEventListener(MouseEvent.CLICK, loadBtnClick);
			start_btn.addEventListener(MouseEvent.CLICK, startResearch);
			
			photo_container_one = new Sprite();
			photo_container_two = new Sprite();
			
			this.addChild(photo_container_one);
			this.addChild(photo_container_two);
		}
		
		private function startResearch(e:MouseEvent):void {
			Main.datacontroller.startResearch();
		}
		
		private function photoLoaded(e:Event):void {
			trace("photoLoaded");
			var tmp_loader:Loader = new Loader();
			tmp_loader.loadBytes(e.target.data);
			switch(e.target) {
				case file_reference_one:
					photo_container_one.addChild(tmp_loader);
					break;
					
				case file_reference_two:
					photo_container_two.addChild(tmp_loader);
					break;
			}
			
			tmp_loader.addEventListener(Event.ADDED, function(e:Event):void {
				tmp_loader.width = 300;
				tmp_loader.scaleY = tmp_loader.scaleX;
			});
			
		}
		
		private function photoSelected(e:Event):void {
			trace("photoSelected");
			switch(e.target) {
				case file_reference_one:
					file_reference_one.load();
					break;
					
				case file_reference_two:
					file_reference_two.load();
					break;
			}
		}
		
		private function loadBtnClick(e:MouseEvent):void {
			switch(e.target) {
				case load_btn_one:
					file_reference_one.browse(new Array(file_reference_type));
					break;
					
				case load_btn_two:
					file_reference_two.browse(new Array(file_reference_type));
					break;
			}
		}
		
		public function drawDebugPoints(data:Object, width_:int, height_:int):Boolean {
			trace(width_, " ", height_);
			
			var output_bool:Boolean = true;
			
			if (data.center != null) {
				var tmp_rect:Rectangle_NS = new Rectangle_NS();
				this.addChild(tmp_rect);
				tmp_rect.x = data.center.x * width_ / 100;
				tmp_rect.y = data.center.y * height_ / 100;
				tmp_rect.width = data.width * width_ / 100;
				tmp_rect.height = data.height * height_ / 100;
			}
			
			if (data.mouth_left != null) {
				var tmp_point:Point_NS = new Point_NS();
				this.addChild(tmp_point);
				tmp_point.x = data.mouth_left.x * width_ / 100;
				tmp_point.y = data.mouth_left.y * height_ / 100;
			}
			
			if (data.mouth_right != null) {
				tmp_point = new Point_NS();
				this.addChild(tmp_point);
				tmp_point.x = data.mouth_right.x * width_ / 100;
				tmp_point.y = data.mouth_right.y * height_ / 100;
			}
			
			if (data.ear_left != null) {
				tmp_point = new Point_NS();
				this.addChild(tmp_point);
				tmp_point.x = data.ear_left.x * width_ / 100;
				tmp_point.y = data.ear_left.y * height_ / 100;
			}
			
			if (data.ear_right != null) {
				tmp_point = new Point_NS();
				this.addChild(tmp_point);
				tmp_point.x = data.ear_right.x * width_ / 100;
				tmp_point.y = data.ear_right.y * height_ / 100;
			}
			
			if (data.eye_left != null) {
				tmp_point = new Point_NS();
				this.addChild(tmp_point);
				tmp_point.x = data.eye_left.x * width_ / 100;
				tmp_point.y = data.eye_left.y * height_ / 100;
			}
			
			if (data.eye_right != null) {
				tmp_point = new Point_NS();
				this.addChild(tmp_point);
				tmp_point.x = data.eye_right.x * width_ / 100;
				tmp_point.y = data.eye_right.y * height_ / 100;
			}
			
			if (data.mouth_center != null) {
				tmp_point = new Point_NS();
				this.addChild(tmp_point);
				tmp_point.x = data.mouth_center.x * width_ / 100;
				tmp_point.y = data.mouth_center.y * height_ / 100;
			}
			
			if (data.nose != null) {
				tmp_point = new Point_NS();
				this.addChild(tmp_point);
				tmp_point.x = data.nose.x * width_ / 100;
				tmp_point.y = data.nose.y * height_ / 100;
			}
			
			return output_bool;
		}
	}

}